/*jslint vars: true, forin: true, sloppy: true */
/*global angular: false, ocDialog, DM */
ocDialog.directive('ocDialogOpen', function (ocDialog, $parse) {
	// Try to get the dialog name if the directive DOM is in a Dialog.

	/**
	 * opt.name Name of dialog factory name.
	 * opt.data	Data object for transmiting to child DI.
	 */
	return {
		restrict: 'A',
		scope: {
			opt: "=ocDialogOpen"
		},
		link: function (scope, element, attr) {
			element.on("click", function () {
				var o = $parse(attr.ocDialogOpen)(scope.$parent), isBlocked = true;
				
				if (angular.isDefined(scope.opt.isAutoBlock)) {
					isBlocked = !!scope.opt.isAutoBlock;
				}

				ocDialog.open({
					name: o.name,
					data: o.data
				}, isBlocked ? DM.queryDialogByElement(element) : null);
			});
		}
	};
});
